public class MileageUtil {
	static final Integer MAX_MILES_PER_DAY = 500;

	public static void areMilesAllowed(Mileage_second__c[] miles){
		Double totalMiles = 0;
		String createdbyId = UserInfo.getUserId();

		/* Adds the miles created in previous requests for today */
		for(Mileage_second__c mq:[SELECT Miles__c FROM Mileage_second__c
				WHERE Date__c = TODAY
				AND Mileage_second__c.createdById = :createdbyId
				AND Miles__c != null]) {
			totalMiles += mq.Miles__c;
		}
		/* Totals the miles in the request */
		for (Mileage_second__c m:miles) {
			totalMiles += m.Miles__c;
			if(totalMiles > MAX_MILES_PER_DAY)
				m.addError('Mileage request exceeda daily limit:' + MAX_MILES_PER_DAY);
		}
	}
}